﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NLBS.Entity;
using System.Data;

namespace NLBS.Data
{
    public partial class SqlDataProvider : DbProvider
    {
        #region VisitorRecord

        public override IList<VisitorRecord> GetVisitorRecord()
        {

            DbCommand dbCommand = db.GetSqlStringCommand("SELECT top 100 vr_id, vr_ip, vr_os, vr_browser, vr_time, vr_referer, vr_target FROM dbo.blog_VisitorRecord");

            IList<VisitorRecord> lists = new List<VisitorRecord>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    VisitorRecord record = CommonDataProvider.PopulateVisitorRecordFromIDataReader(dataReader);
                    lists.Add(record);
                }
            }
            return lists;
        }

        public override void InsertVisitorRecord(VisitorRecord visitor)
        {

            DbCommand dbCommand = db.GetSqlStringCommand("INSERT INTO [dbo].[blog_VisitorRecord] ([vr_ip], [vr_os], [vr_browser], [vr_time], [vr_referer], [vr_target]) VALUES (@vr_ip, @vr_os, @vr_browser, @vr_time, @vr_referer, @vr_target)");
            db.AddInParameter(dbCommand, "vr_ip", DbType.String, visitor.IP);
            db.AddInParameter(dbCommand, "vr_os", DbType.String, visitor.OS);
            db.AddInParameter(dbCommand, "vr_browser", DbType.String, visitor.Browser);
            db.AddInParameter(dbCommand, "vr_time", DbType.DateTime, visitor.Time);
            db.AddInParameter(dbCommand, "vr_referer", DbType.String, visitor.Referer);
            db.AddInParameter(dbCommand, "vr_target", DbType.String, visitor.Target);
            db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}
